#!/bin/sh

###############################################################################
##
##  3D-RISM EXAMPLE
##
###############################################################################

EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "This example shows how to use 3D-RISM calculation"
$ECHO "1) H2O molecule in NaCl(aq), and"
$ECHO "2) HCHO molecule in water."
$ECHO

# set the needed environment variables
. ../../../environment_variables

# required executables, pseudopotentials and molecules
BIN_LIST="pw.x pprism.x"
PSEUDO_LIST="H.pbe-van_ak.UPF C.pbe-van_ak.UPF O.pbe-van_ak.UPF"
MOL_LIST="H2O.spc.MOL Na+.aq.MOL Cl-.aq.MOL Ethanol.oplsua.MOL"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# check for molecules
for FILE in $MOL_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Copy $FILE to $PSEUDO_DIR...\c"
       cp $EXAMPLE_DIR/MOL/$FILE $PSEUDO_DIR 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
PP_COMMAND="$PARA_PREFIX $BIN_DIR/pprism.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x     as: $PW_COMMAND"
$ECHO "  running pprism.x as: $PP_COMMAND"
$ECHO

# 3D-RISM calculation for H2O in NaCl(aq)
cat > H2O.NaCl_aq.in << EOF
 &control
    calculation = 'scf'
    prefix      = 'H2O.NaCl_aq'
    pseudo_dir  = '$PSEUDO_DIR/'
    outdir      = '$TMP_DIR/'
    trism       = .true.
 /
 &system
    ibrav       = 0
    ntyp        = 2
    nat         = 3
    ecutwfc     = 25.0
    ecutrho     = 225.0
    occupations = 'fixed'
 /
 &electrons
    mixing_beta = 0.4
 /
 &ions
    ion_dynamics = 'bfgs'
 /
 &rism
    nsolv    = 3
    closure  = 'kh'
    tempv    = 300.0  ! Kelvin
    ecutsolv = 144.0  ! Rydberg
    ! Lennard-Jones for each atom (SPC model)
    solute_epsilon(1) = 0.1554  ! kcal/mol
    solute_sigma(  1) = 3.1660  ! angstrom
    solute_epsilon(2) = 0.0460
    solute_sigma(  2) = 1.0000
    ! 1D-RISM's setting
    starting1d      = 'zero'
    rism1d_conv_thr = 1.0e-8
    rism1d_maxstep  = 10000
    mdiis1d_size    = 20
    mdiis1d_step    = 0.5
    ! 3D-RISM's setting
    starting3d      = 'zero'
    rism3d_maxstep  = 2000
    rism3d_conv_thr = 1.0e-5
    mdiis3d_size    = 10
    mdiis3d_step    = 0.8
 /
ATOMIC_SPECIES
 O  -1.0  O.pbe-van_ak.UPF
 H  -1.0  H.pbe-van_ak.UPF
CELL_PARAMETERS angstrom
   10.00000  0.00000  0.00000
    0.0000  10.00000  0.00000
    0.0000   0.00000 10.00000
ATOMIC_POSITIONS {angstrom}
 O  5.00000  5.00000  5.00000
 H  5.81649  5.57736  5.00000
 H  4.18351  5.57736  5.00000
K_POINTS {gamma}
SOLVENTS {mol/L}
 H2O  -1.0  H2O.spc.MOL
 Na+   0.5  Na+.aq.MOL
 Cl-   0.5  Cl-.aq.MOL
EOF
$ECHO "  running the 3D-RISM calculation for H2O in NaCl(aq)...\c"
$PW_COMMAND < H2O.NaCl_aq.in > H2O.NaCl_aq.out
check_failure $?
cp -f $TMP_DIR/H2O.NaCl_aq.1drism ./
cp -f $TMP_DIR/H2O.NaCl_aq.xml ./
$ECHO " done"

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/H2O.NaCl_aq*
$ECHO " done"

# 3D-RISM calculation for HCHO in water
cat > HCHO.aq.in << EOF
 &control
    calculation = 'scf'
    prefix      = 'HCHO.aq'
    pseudo_dir  = '$PSEUDO_DIR/'
    outdir      = '$TMP_DIR/'
    trism       = .true.
 /
 &system
    ibrav       = 0
    ntyp        = 3
    nat         = 4
    ecutwfc     = 25.0
    ecutrho     = 225.0
    occupations = 'fixed'
 /
 &electrons
    mixing_beta = 0.4
 /
 &rism
    nsolv    = 1
    closure  = 'kh'
    tempv    = 300.0  ! Kelvin
    ecutsolv = 144.0  ! Rydberg
    ! Lennard-Jones
    solute_lj(1) = 'opls-aa'
    solute_lj(2) = 'opls-aa'
    solute_lj(3) = 'opls-aa'
 /
ATOMIC_SPECIES
 C  -1.0  C.pbe-van_ak.UPF
 O  -1.0  O.pbe-van_ak.UPF
 H  -1.0  H.pbe-van_ak.UPF
CELL_PARAMETERS angstrom
   10.00000  0.00000  0.00000
    0.0000  10.00000  0.00000
    0.0000   0.00000 10.00000
ATOMIC_POSITIONS {angstrom}
 C  5.000  5.609  5.000
 O  5.000  4.425  5.000
 H  4.075  6.191  5.000
 H  5.925  6.191  5.000
K_POINTS {gamma}
SOLVENTS {g/cm^3}
 H2O  1.0  H2O.spc.MOL
EOF
$ECHO "  running the 3D-RISM calculation for HCHO in water...\c"
$PW_COMMAND < HCHO.aq.in > HCHO.aq.out
check_failure $?
cp -f $TMP_DIR/HCHO.aq.1drism ./
cp -f $TMP_DIR/HCHO.aq.xml ./
$ECHO " done"

# plot solvent distribution
cat > HCHO.aq.post.in << EOF
 &inputpp
    prefix  = 'HCHO.aq'
    outdir  = '$TMP_DIR/'
    lpunch  = .true.
 /
 &plot
    iflag         = 3  ! 3D plot
    output_format = 6  ! Gaussian's CUBE
 /
EOF
$ECHO "  running the postprocessing to plot solvent...\c"
$PP_COMMAND < HCHO.aq.post.in > HCHO.aq.post.out
check_failure $?
$ECHO " done"

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/HCHO.aq*
$ECHO " done"

$ECHO
$ECHO "run_example_3D-RISM: done"
